<?php
use Psr\Http\Message\StreamInterface;
use We7\Http\Stream;


function stream_for($resource = '', array $options = array())
{
	if (is_scalar($resource)) {
		$stream = fopen('php://temp', 'r+');
		if ($resource !== '') {
			fwrite($stream, $resource);
			fseek($stream, 0);
		}
		return new Stream($stream, $options);
	}

	switch (gettype($resource)) {
		case 'resource':
			return new Stream($resource, $options);
		case 'object':
			if ($resource instanceof StreamInterface) {
				return $resource;
			} elseif (method_exists($resource, '__toString')) {
				return stream_for((string) $resource, $options);
			}
			break;
		case 'NULL':
			return new Stream(fopen('php://temp', 'r+'), $options);
	}

	throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource));
}